package com.ruoyi.core.service.impl;

import java.util.Date;
import java.util.List;


import com.ruoyi.common.entity.CoreEntity.Members;
import com.ruoyi.common.entity.CoreEntity.ShippingAddresses;
import com.ruoyi.common.entity.TransactionEntity.Sku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.core.mapper.MembersMapper;

import com.ruoyi.core.service.IMembersService;

/**
 * 会员信息Service业务层处理
 * 
 * @author 亢哲
 * @date 2025-08-07
 */
@Service
public class MembersServiceImpl implements IMembersService 
{
    @Autowired
    private MembersMapper membersMapper;


    /**
     * 修改会员手机号
     * @param memberId
     * @param phone
     * @return
     */
    @Override
    public int updatePhone(Long memberId, String phone) {
        //1.参数验证(手机号不能为空)(.trim()去除两端空白符.isEmpty()检查处理后的字符串是否为空字符串)
        if (memberId == null || phone == null || phone.trim().isEmpty()) {
            //抛出异常错误信息（如果错误可以方便查找什么错误）
            throw new IllegalArgumentException("手机号不能为空");
        }

        //2.验证手机号格式
        if(!phone.matches("^1[3-9]\\d{9}$")){
            throw new IllegalArgumentException("手机号格式错误");
        }

        //3.检查手机号是否被其他用户使用
        Members queryMember = new Members();
        queryMember.setPhone(phone);
        List<Members> existingMembers = membersMapper.selectMembersList(queryMember);
        for (Members members : existingMembers) {
            if(!members.getId().equals(memberId)){
               throw new IllegalArgumentException("手机号已被其他用户使用");
            }
        }

        //4.执行更新
        Members updateMember = new Members();
        updateMember.setId(memberId);
        updateMember.setPhone(phone);
        updateMember.setCreatedAt(new Date());

        return membersMapper.updateMembers(updateMember);
    }



    /**
     * 修改会员支付密码
     * @param memberId
     * @param payPassword
     * @return
     */
    @Override
    public int updatePayPassword(Long memberId, String payPassword) {
        //1.参数验证(由于支付密码我设置可以为空，所以这里是可以存在修改自己为空的支付密码的，如果支付密码为空的话不就相当于修改自己为空的支付密码)
        if(memberId == null){
            //依旧熟悉的抛出异常报错信息
            throw new IllegalArgumentException("会员ID不能为空");
        }

        //2.验证支付密码长度
        if(!payPassword.matches("^\\d{6}$")){
            throw new IllegalArgumentException("支付密码必须是6位数字");
        }

        //3.执行更新
        Members updateMember =new Members();
        updateMember.setId(memberId);
        updateMember.setPayPassword(payPassword);
        updateMember.setUpdatedAt(new Date());


        return membersMapper.updateMembers(updateMember);
    }


    /**
     * 查询会员信息
     *
     * @param username 会员信息主键
     * @return 会员信息
     */
    @Override
    public Members selectMembersByUsername(String username) {
        return membersMapper.selectMembersByUsername(username);
    }


    /**
     * 查询会员的收货地址
     * @param membersId
     * @return
     */
    @Override
    public List<ShippingAddresses> selectShippingAddressesByMembersId(Long membersId) {
        return membersMapper.selectShippingAddressesByMembersId(membersId);
    }

    /**
     * 查询会员信息
     * 
     * @param id 会员信息主键
     * @return 会员信息
     */
    @Override
    public Members selectMembersById(Long id)
    {
        return membersMapper.selectMembersById(id);
    }

    /**
     * 查询会员信息列表
     * 
     * @param members 会员信息
     * @return 会员信息
     */
    @Override
    public List<Members> selectMembersList(Members members)
    {
        return membersMapper.selectMembersList(members);
    }

    /**
     * 查询会员收藏的商品
     * @param membersId
     * @return
     */
    @Override
    public List<Sku> selectSkuListByMembersId(Long membersId) {
        return membersMapper.selectSkuListByMembersId(membersId);
    }

    /**
     * 新增会员信息
     * 
     * @param members 会员信息
     * @return 结果
     */
    @Override
    public int insertMembers(Members members)
    {
        return membersMapper.insertMembers(members);
    }

    /**
     * 修改会员信息
     * 
     * @param members 会员信息
     * @return 结果
     */
    @Override
    public int updateMembers(Members members)
    {
        return membersMapper.updateMembers(members);
    }

    /**
     * 批量删除会员信息
     * 
     * @param ids 需要删除的会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMembersByIds(Long[] ids)
    {
        return membersMapper.deleteMembersByIds(ids);
    }

    /**
     * 删除会员信息信息
     * 
     * @param id 会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMembersById(Long id)
    {
        return membersMapper.deleteMembersById(id);
    }


//    Sxz
    @Override
    public Members getMemberWithDetailsById(Long userId) {
        return membersMapper.selectMemberWithDetailsById(userId);
    }
}
